<template>
  <div class="login">
    <div class="container">
      <div class="login-box">
        <div class="title">后台管理</div>
        <el-form :model="user" :rules="rules" ref="userForm" label-width="100px">
          <el-form-item style="margin-right: 100px" prop="username">
            <el-input v-model="user.username" prefix-icon="el-icon-user" placeholder="username"></el-input>
          </el-form-item>
          <el-form-item style="margin-right: 100px" prop="password">
            <el-input v-model="user.password" prefix-icon="el-icon-lock" show-password
                      placeholder="password"></el-input>
          </el-form-item>
          <div style="margin: 50px auto 0; width: 300px; text-align: center">
            <el-button style="margin-right: 10px" type="primary" @click="loginUser('userForm')">登录</el-button>
            <el-button style="margin-left: 10px" type="info">重置</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// import Cookie from 'cookie'
// import permission from "@/store/permission";

import {restRouter} from "@/router";

export default {
  name: 'admin-login',

  data() {
    return {
      user: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
        ]
      }
    }
  },

  mounted() {
  },

  methods: {
    //用户登录
    loginUser(userForm) {
      this.$refs[userForm].validate(valid => {
        if (valid) {
          this.request.post('/login', this.user).then((res) => {
            this.$cookie.set('username', res.username)
            this.$message.success("登录成功")

            //重置路由
            restRouter()
            //触发vuex中的获取路由菜单方法
            this.$store.dispatch('permission/GenerateRoutes')

            //跳转至首页
            this.$router.replace('/home')
          }).catch(e => {
            this.$message.error(e)
          })
        } else {
          this.$message.error('请输入正确的用户名或密码')
          return false
        }
      })
    },

  }
}
</script>

<style lang="scss" scoped>
@import '../assets/scss/config.scss';

.login {
  width: 100%;
  // height: 100%;
  min-height: 100%;
  height: 100vh;
  background-color: #283443;
  overflow: hidden;

  .login-box {
    margin: 200px auto;
    width: 500px;
    height: 350px;
    background: #00000050;
    border-radius: 15px;
    padding: 20px;
  }

  .title {
    width: 300px;
    height: 70px;
    margin: 0 auto 30px;
    font-size: 36px;
    text-align: center;
    color: $colorA;
  }
}
</style>
